package com.zhiyou100.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.Customer;
import com.zhiyou100.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    CustomerService customerService;

    @RequestMapping("/list")
    public String listCustomer(@RequestParam Map map, @RequestParam(defaultValue = "1") int pageNum, HttpServletRequest request){
        System.out.println("CustomerController-map : "+map);
        PageHelper.startPage(pageNum, 5);
        List<Customer> customers = customerService.listCustomer(map);
        PageInfo<Customer> pageInfo = new PageInfo<Customer>(customers);
        request.setAttribute("map",map);
        request.setAttribute("customers",customers);
        request.setAttribute("pageInfo",pageInfo);
        return "forward:/behind/customer/listCustomer.jsp";
    }


    @RequestMapping("/addCustomer")
    public String addCustomer(@RequestParam(defaultValue = "0") String strId,Customer customer){
        int id = Integer.parseInt(strId);
        if(id == 0){
            System.out.println("要添加的customer是   :   "+customer);
            int i = customerService.addCustomer(customer);
            System.out.println("添加customer返回的受影响行数是   : "+i);
        }else {
            System.out.println("要更新的customerId是 : "+id);
            customer.setId(id);
            int  i  = customerService.updateCustomer(customer);
            System.out.println("更新customer返回的受影响行数是   : "+i);
        }
        return "redirect:/customer/list.do";

    }



    @RequestMapping("/lookCustomer")
    public String lookCustomer(int id,HttpServletRequest request){
        System.out.println("要查看修改的id是 : "+id);
        Customer customer =  customerService.lookCustomer(id);
        request.setAttribute("customer",customer);
        return "forward:/behind/customer/addCustomer.jsp";

    }
    @RequestMapping("/deleteAll")
    public String deleteAll(int[] ids){
        // System.out.println("要删除的数组是   :   "+ids.length);
        List<Integer> customerIds = new ArrayList<>();
        for (int id:ids) {
            customerIds.add(id);
        }
        // System.out.println("list集合的大小是  : "+videoIds.size());
        int i = customerService.deleteAll(customerIds);
        System.out.println("批量删除后返回的受影响行数是   :   "+i);
        return "redirect:/customer/list.do";
    }
    @RequestMapping("/delete")
    private String delete(int id){
        System.out.println("要删除的id是    :    "+id);
        int i = customerService.delete(id);
        System.out.println("单个删除后返回的受影响行数是   :   "+i);
        return "redirect:/customer/list.do";

    }

}
